﻿using System;
using Token.Piker.Common;
using System.IO;

namespace Token.Piker.Web.Admin.Activity
{
    public partial class Modify : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    int ID = (Convert.ToInt32(Request.Params["id"]));
                    ViewState["ID"] = ID;
                    ShowInfo(ID);
                }
            }
        }

        private void ShowInfo(int ID)
        {
            Token.Piker.BLL.ActivityBLL bll = new Token.Piker.BLL.ActivityBLL();
            Token.Piker.Model.Activity model = bll.GetModel(ID);
            this.txtTheme.Text = model.Theme;
            this.txtDescrip.Text = model.Descrip;
            this.txtAward.Text = model.Award;
            this.txtStartTime.Text = ((DateTime)model.StartTime).ToShortDateString();
            this.txtEndTime.Text = ((DateTime)model.EndTime).ToShortDateString();
            this.txtPosition.Text = model.Position;
            this.ddlFlag.SelectedIndex = Convert.ToInt32(model.Flag);
        }

        protected void btnModify_Click(object sender, EventArgs e)
        {
            if (Session[GlobalConstants.PIKER_ADMIN] == null)
            {
                MessageBox.ShowAndRedirect(this.Page, "网络连接超时，请重新登录！", ResolveClientUrl("~/Admin/Login.aspx"));
                return;
            }

            int Creator = Convert.ToInt32(Session[GlobalConstants.PIKER_ADMIN]);
            string Theme = this.txtTheme.Text;
            string Descrip = this.txtDescrip.Text;
            string Award = this.txtAward.Text;
            DateTime StartTime = DateTime.Parse(this.txtStartTime.Text);
            DateTime EndTime = DateTime.Parse(this.txtEndTime.Text);
            string Position = this.txtPosition.Text;
            string BackgroundImg=string.Empty;
            if (fileUp.HasFile)
            {
                string fileName = fileUp.FileName;
                string ext = Path.GetExtension(fileName);
                string path = "~/Admin/uploads/activity/";
                path = path + DateTime.Now.ToString("yyyyMMdd") + ext;
                BackgroundImg = path;
                fileUp.SaveAs(Server.MapPath(path));
            }
            int Flag = int.Parse(ddlFlag.SelectedValue);

            Token.Piker.BLL.ActivityBLL bll = new Token.Piker.BLL.ActivityBLL();
            int ID = Convert.ToInt32(ViewState["ID"]);
            Token.Piker.Model.Activity model = bll.GetModel(ID);
            model.Creator = Creator;
            model.Theme = Theme;
            model.Descrip = Descrip;
            model.Award = Award;
            model.StartTime = StartTime;
            model.EndTime = EndTime;
            model.Position = Position;
            if (BackgroundImg != string.Empty)
            {
                model.BackgroundImg = BackgroundImg;
            }
            model.Flag = Flag;

            bll.Update(model);
            MessageBox.ShowAndRedirect(this, "修改成功！", "List.aspx");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("List.aspx");
        }
    }
}